package com.bs.redis.controller;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author bingshao
 * @date 2023/3/15
 **/
@RestController
@RequestMapping("/hyperLogLog")
public class HyperLogLogController {

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @PostMapping("/add")
    public void add() {
        redisTemplate.opsForHyperLogLog().add("hyperLogLog_name1", "冰少");
        redisTemplate.opsForHyperLogLog().add("hyperLogLog_name1", "妃妃");
        redisTemplate.opsForHyperLogLog().add("hyperLogLog_name1", "云澈");
        redisTemplate.opsForHyperLogLog().add("hyperLogLog_name1", "茉莉");
        redisTemplate.opsForHyperLogLog().add("hyperLogLog_name1", "倾月");
        redisTemplate.opsForHyperLogLog().add("hyperLogLog_name1", "彩脂");
        redisTemplate.opsForHyperLogLog().add("hyperLogLog_name1", "冰少");
        redisTemplate.opsForHyperLogLog().add("hyperLogLog_name1", "妃妃");

        redisTemplate.opsForHyperLogLog().add("hyperLogLog_name2", "苍月");
        redisTemplate.opsForHyperLogLog().add("hyperLogLog_name2", "凤兒儿");
        redisTemplate.opsForHyperLogLog().add("hyperLogLog_name2", "小妖后");
        redisTemplate.opsForHyperLogLog().add("hyperLogLog_name2", "冰少");
        redisTemplate.opsForHyperLogLog().add("hyperLogLog_name2", "茉莉");
        redisTemplate.opsForHyperLogLog().add("hyperLogLog_name2", "甄姬");
        redisTemplate.opsForHyperLogLog().add("hyperLogLog_name2", "苍月");
        redisTemplate.opsForHyperLogLog().add("hyperLogLog_name2", "甄姬");
    }

    @GetMapping("/size")
    public void size() {
        Long size = redisTemplate.opsForHyperLogLog().size("hyperLogLog_name1", "hyperLogLog_name2");
        System.out.println(size); // 10 两个键总独立数和

        Long size1 = redisTemplate.opsForHyperLogLog().size("hyperLogLog_name_total");
        System.out.println(size1); // 10    同上
    }

    @GetMapping("/merge")
    public void merge() {
        // 合并独立个数
        redisTemplate.opsForHyperLogLog().union("hyperLogLog_name_total", "hyperLogLog_name1", "hyperLogLog_name2");
    }
}
